using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class UserControls_ASPPager : System.Web.UI.UserControl
{
    private const int MaxiumPageCount = 10;
    
   

    protected void Page_Load(object sender, EventArgs e)
    {  
        InitComponentsState();
    }

    private int GetStartPage(int CurrentPage, int TotalPage)
    {
        int nStartPage = CurrentPage - (MaxiumPageCount / 2);

        if (nStartPage < 0)
            return 1;
        
        return nStartPage;
    }
    private int GetEndPage(int StartPage, int CurrentPage, int TotalPage)
    {
        int nEndPage = StartPage + MaxiumPageCount - 1;

        if (nEndPage >= TotalPage)
            return TotalPage;

        return nEndPage;
    }

    private void AddPageSelector()
    {
        TableCell td = (TableCell)this.FindControl("PageSelector");

        if (td != null)
        {
            td.Controls.Clear();

            int nCurrentPage = CurrentPage;
            int nTotalPage   = TotalPage;

            int nStartPage  = GetStartPage(nCurrentPage, nTotalPage);
            int nEndPage = GetEndPage(nStartPage, nCurrentPage, nTotalPage);

            for (int i = nStartPage; i <= nEndPage; i++)
            {
                LinkButton btSelector = new LinkButton();

                btSelector.Text = i.ToString() + "  ";
                btSelector.ID = "PageSelector" + i;
                btSelector.Click += new EventHandler(OnPageNumberSelect);
                btSelector.CommandArgument = i.ToString();

                if (i == CurrentPage)
                    btSelector.Enabled = false;
                else
                    btSelector.Enabled = true;
                
                td.Controls.Add(btSelector);
            }
        }
    }

    private void InitComponentsState()
    {
        if (CurrentPage <= 1)
            this.bnPreviousPage.Enabled = false;
        else
            this.bnPreviousPage.Enabled = true;

        if (CurrentPage >= TotalPage)
            this.bnNextPage.Enabled = false;
        else
            this.bnNextPage.Enabled = true;

        if (CurrentPage == 1)
            this.bnFirstPage.Enabled = false;
        else
            this.bnFirstPage.Enabled = true;

        if (CurrentPage >= TotalPage)
            this.bnLastPage.Enabled = false;
        else
            this.bnLastPage.Enabled = true;

       
        AddPageSelector();
        
    }

    private int _PageSize = 10;
    public int PageSize
    {
        get
        {
            try
            {
                return System.Convert.ToInt32(this.ddlPageSize.SelectedValue);
            }
            catch (System.Exception)
            {
                return 10;
            }
        }
        set
        {
            _PageSize = value;
            this.ddlPageSize.SelectedValue = value.ToString();
        }
    }

    
    public int StartIndex
    {
        get
        {
            String s = (String)ViewState[this.ID + ".StartIndex"];
            return ((s == null) ? (CurrentPage - 1) * PageSize : Convert.ToInt32(s));

            
        }
        set
        {
            ViewState[this.ID + ".StartIndex"] = value.ToString();
        }
    }

    private int _CurrentPage;
    public int CurrentPage
    {
        get
        {
            try
            {
                return System.Convert.ToInt32(this.lbCurrentPage.Text);
            }
            catch (System.Exception)
            {
                return 1;
            }
        }
        set
        {
            _CurrentPage = value;
            this.lbCurrentPage.Text = value.ToString();
        }
    }
    private int _TotalPage = 1;
    public int TotalPage
    {
        get 
        {
            try
            {
                return System.Convert.ToInt32(this.lbTotalPage.Text);
            }
            catch (System.Exception)
            {
                return 1;
            }
        }
        set
        {
            _TotalPage = value;
            lbTotalPage.Text = value.ToString();
        }
    }

    private int _TotalRecord = 1;
    public int TotalRecord
    {
        get 
        {
            try
            {
                return System.Convert.ToInt32(this.lbRecordCount.Text);
            }
            catch (System.Exception)
            {
                return 1;
            }
            
        }
        set
        {
            _TotalRecord = value;
            this.lbRecordCount.Text = value.ToString();

            if (value - (value / this.PageSize) * this.PageSize > 0)
                this.TotalPage = value / this.PageSize + 1;
            else
                this.TotalPage = value / this.PageSize;

            InitComponentsState();
        }
    }



    protected void PreviousPage_Click(object sender,EventArgs e)
    {
        try
        {
            if (Convert.ToInt16(this.lbCurrentPage.Text) > 1)
            {
                int i;
                i = Convert.ToInt16(this.lbCurrentPage.Text) - 1;
                this.lbCurrentPage.Text = i.ToString();
                CurrentPage = i;
                StartIndex  = (CurrentPage - 1) * PageSize;
                InitComponentsState();
            }

        }
        catch (System.Exception)
        {
            return;
        }
    }

    protected void FirstPage_Click(object sender, EventArgs e)
    {
        this.lbCurrentPage.Text = "1";
        this.CurrentPage = 1;
        StartIndex = 0;

        InitComponentsState();
    }

    protected void LastPage_Click(object sender, EventArgs e)
    {
        this.lbCurrentPage.Text = this.lbTotalPage.Text;
        this.CurrentPage        = Convert.ToInt16(this.lbTotalPage.Text);
        StartIndex              = (CurrentPage - 1) * PageSize;
        InitComponentsState();
    }

    protected void NextPage_Click(object sender, EventArgs e)
    {
        try
        {
            if (Convert.ToInt16(this.lbCurrentPage.Text) < Convert.ToInt16(this.lbTotalPage.Text))
            {
                int i;
                i = Convert.ToInt16(this.lbCurrentPage.Text) + 1;
                this.lbCurrentPage.Text = i.ToString();
                CurrentPage = i;
                StartIndex  = (CurrentPage - 1) * PageSize;
                InitComponentsState();
            }
            
        }
        catch (System.Exception)
        {
            return;
        }
    }

    protected void OnPageSizeChange(object sender, EventArgs e)
    {
        this.PageSize   = Convert.ToInt32(this.ddlPageSize.SelectedValue);

        if (this.TotalRecord - (this.TotalRecord / this.PageSize) * this.PageSize > 0)
            this.TotalPage = this.TotalRecord / this.PageSize + 1;
        else
            this.TotalPage = this.TotalRecord / this.PageSize;

        if ((StartIndex / PageSize) + 1 < TotalPage)
            this.CurrentPage = (StartIndex / PageSize) + 1;
        else
            this.CurrentPage = TotalPage;

        InitComponentsState();
    }

    protected void OnPageNumberSelect(object sender, EventArgs e)
    {
        LinkButton btSender = (LinkButton)sender;
        string strCommandName = btSender.CommandArgument;

        int i;
        try
        {
            i = Convert.ToInt32(strCommandName);
            this.lbCurrentPage.Text = i.ToString();
            CurrentPage = i;
            StartIndex = (CurrentPage - 1) * PageSize;
            InitComponentsState();
        }
        catch (Exception ex)
        {
            return;
        }
    }
    
}
